﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fierce.Core.Aop;
using Fierce.Entity.Basic;
using Fierce.Utility.Helper;

namespace Fierce.Core.Service.Basic
{
    public partial class BasicUserService : BaseService<BasicUser>
    {
        public virtual List<BasicUser> Grid(int rows, int page, string search, out int count)
        {
            var entities = db.BasicUser.Query(m => !m.IsSuper);

            if (!string.IsNullOrEmpty(search))
            {
                entities = entities.Where(m => m.UserName.Contains(search) || m.Name.Contains(search));
            }

            return db.BasicUser.Pager(rows, page, out count, entities).ToList();
        }

        public virtual ServiceResult SignIn(string username, string password)
        {
            password = EncryptHelper.Md5(password);

            var entity = db.BasicUser.Get(m => m.UserName == username && m.PassWord == password);

            if (entity != null)
            {
                if (entity.IsEnabled)
                {
                    return SuccessResult(data: entity);
                }
                else { return FailureResult("用户被禁用"); }
            }
            else { return FailureResult("帐号或密码错误"); }
        }

        public override ServiceResult Save(BasicUser entity)
        {
            if (string.IsNullOrEmpty(entity.ID))
            {
                entity.PassWord = EncryptHelper.Md5(entity.PassWord);

                db.BasicUser.Added(entity);

                // 取得默认角色设置
                var defrole = db.BasicSetting.Get(m => m.Tag == "DefRole");

                if (defrole != null && !string.IsNullOrEmpty(defrole.Content))
                {
                    // 为用户设置默认角色
                    BasicUserRole bur = new BasicUserRole();
                    bur.RoleID = defrole.Content;
                    bur.UserID = entity.ID;
                    db.BasicUserRole.Added(bur);
                }

                return SaveAndResult("添加成功");
            }
            else
            {
                var old = db.BasicUser.Get(entity.ID);

                entity.UserName = old.UserName;
                entity.PassWord = old.PassWord;
                db.BasicUser.Modified(entity);

                return SaveAndResult("修改成功");
            }
        }

        public virtual ServiceResult ResetPwd(BasicUser model, string oper)
        {
            BasicUser entity = db.BasicUser.Get(model.ID);

            if (entity != null)
            {
                entity.PassWord = EncryptHelper.Md5(model.PassWord);
                db.BasicUser.Modified(entity);
                ServiceResult result = SaveAndResult("重置成功");
                //日志记录
                result.Data = new
                {
                    Action = "ResetPwd",
                    Oper = oper,
                    UserName = entity.UserName
                };
                LogHelper.Info(result.Serialize()); return result;
            }

            return FailureResult();
        }

        public virtual ServiceResult UpdatePwd(string id, string oldpwd, string newpwd)
        {
            BasicUser entity = db.BasicUser.Get(id);

            if (entity != null)
            {
                if (entity.PassWord == EncryptHelper.Md5(oldpwd))
                {
                    entity.PassWord = EncryptHelper.Md5(newpwd);
                    db.BasicUser.Modified(entity);
                    ServiceResult result = SaveAndResult("修改成功");
                    //日志记录
                    result.Data = new
                    {
                        Action = "UpdatePwd",
                        Oper = entity.UserName
                    };
                    LogHelper.Info(result.Serialize()); return result;
                }
                else { return FailureResult("密码不正确"); }
            }

            return FailureResult();
        }

        public virtual ServiceResult EnableOrDisable(string id, bool bit)
        {
            BasicUser entity = db.BasicUser.Get(id);

            if (entity != null)
            {
                if (entity.IsEnabled != bit)
                {
                    entity.IsEnabled = bit;

                    db.BasicUser.Modified(entity);

                    return SaveAndResult(bit ? "启用成功" : "禁用成功");
                }
                else
                {
                    return FailureResult(bit ? "已经是启用状态" : "已经是禁用状态");
                }
            }

            return FailureResult();
        }
    }
}
